import 'dart:math';
import 'package:get/get.dart';

class MessageIndexController extends GetxController {
  MessageIndexController();

  // 头像列表
  final List<String> avatarUrls = [
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_1.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_2.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_3.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_4.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_5.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_6.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_7.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_8.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_9.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_10.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_11.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_12.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_13.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_14.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_15.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_16.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_17.webp',
    'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_18.webp',
  ];

  // 消息列表
  RxList<MessageItem> messageList = <MessageItem>[].obs;

  _initData() {
    // 初始化消息数据
    _loadMessageData();
    update(["message_index"]);
  }

  // 加载消息数据
  void _loadMessageData() {
    messageList.value = [
      MessageItem(
        id: '1',
        type: MessageType.user,
        name: '卡巴拉',
        avatar: getRandomAvatar(),
        content: '让我猜猜你是谁？大美哥',
        time: '11月5日 16:44',
        unreadCount: 0,
      ),
      MessageItem(
        id: '2',
        type: MessageType.user,
        name: '掉了牙的猪',
        avatar: getRandomAvatar(),
        content: '我是听懂',
        time: '11月5日 16:33',
        unreadCount: 0,
      ),
    ];
  }

  // 获取随机头像
  String getRandomAvatar() {
    return avatarUrls[Random().nextInt(avatarUrls.length)];
  }

  // 点击消息项
  void onMessageTap(MessageItem message) {
    Get.toNamed('/message_message_chat', arguments: {
      'name': message.name,
      'avatar': message.avatar,
      'userId': message.id,
    });
  }

  @override
  void onReady() {
    super.onReady();
    _initData();
  }
}

// 消息类型
enum MessageType {
  user,      // 用户消息
  comment,   // 评论
  follow,    // 关注
  system,    // 系统
  official,  // 官方
}

// 消息项模型
class MessageItem {
  final String id;
  final MessageType type;
  final String name;
  final String avatar;
  final String content;
  final String time;
  final int unreadCount;

  MessageItem({
    required this.id,
    required this.type,
    required this.name,
    required this.avatar,
    required this.content,
    required this.time,
    this.unreadCount = 0,
  });
}
